import {handler, view, BondViewGroup} from "adajs";
import LodgeService from "./state.js";
import Bar from "../../../../controls/bar";
import Form from "./../../../../controls/form";
import Toast from "./../../../../controls/toast";
import Loading from "./../../../../controls/loading";
import HosDetail from "../../../../controls/detail/hospital";

@view({
    className: "adminmedicaladd",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: LodgeService
    }
})
class Lodge extends BondViewGroup {
    tags() {
        return {
            bar: Bar,
            form: Form,
            detail: HosDetail
        }
    }

    @handler("bar-add")
    barAdd({data}) {
        console.log(data);
    }

    @handler("submit")
    submit() {
        let base = this.getChildrenByType(Form)[0];
        let form = this.getChildrenByType(Form)[1];
        let detail = this.getChildrenByType(HosDetail)[0];
        base.getValue().then(values => {
            if (!values.ltzid) {
                this.addChild(Toast, {parameter: {content: "先选择人员"}});
            } else {
                form.check().then(result => {
                    if (result) {
                        form.getValue().then(values => {
                            this.addChild(Loading).then(loading => {
                                let data = detail.getCurrentState();
                                values.ywid = data.newsInfo.id;
                                values.zflx = 0;
                                Object.assign(values, data.newsInfo);
                                this.context.request.post("/api/admin/lgb/consolation/insert", values).then(info => {
                                    if (info.code === "1") {
                                        loading.showSuccess("添加成功");
                                        loading.close().then(() => {
                                            window.history.back();
                                        });
                                    } else {
                                        loading.showError(info.msg);
                                        loading.close();
                                    }
                                });
                            });
                        });
                    }
                });
            }
        });
    }

    @handler("selected")
    selected({data}) {
        this.commit('setList', data);
    }
}

export default Lodge;